package edu.API;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class ListPra {
    public static void main(String[] args) {
//        addEle();
        listInter();
    }

    static void listInter(){
        Student stu = new Student("tom", 20);
        Student stu2 = new Student("tom", 21);
        Student stu3 = new Student("jack", 20);
        Student stu4 = new Student("rose", 20);

        //将多个学生保存到一起
        List<Student> list = new ArrayList<>();
        list.add(stu);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        //获取全部学生信息  遍历
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println("student = " + student);
        }
        System.out.println("----------------------------");
        //foreach
        for (Student student : list) {
            System.out.println("student = " + student);
        }
        System.out.println("----------------------------");
        //利用迭代器    将集合 转成 Iterator对象  while遍历
        Iterator<Student> iterator = list.iterator();
        //hasNext  判断当前迭代器中是否还有元素
        while (iterator.hasNext()){
            //获取迭代器元素
            Student student = iterator.next();
            System.out.println("student = " + student);
        }
        System.out.println("----------------------------");
        //函数式编程实现
        Consumer<Student> consumer = new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        };
        list.forEach(consumer);
//        list.forEach(student -> System.out.println(student));
//        list.stream().forEach(student -> System.out.println(student));
    }


    static void addEle(){
        //创建一个空集合,不指定泛型时，默认当前集合时Object类型
        ArrayList arrayList = new ArrayList();
        //一般创建集合时，要求指定泛型   只需要指定前面<>即可
        ArrayList<String> array = new ArrayList<>();
        //利用动态
        List<String> list = new ArrayList<>();

        //添加数据
        list.add("001");
        list.add("002");
        list.add("003");
        System.out.println("list = " + list);
        //获取数据   下标
        String s = list.get(0);
        System.out.println("s = " + s);
        //修改数据
        list.set(1,"202");
        System.out.println("list = " + list);
        //删除数据
        list.remove(1);
//        list.remove("202");
        System.out.println("list = " + list);
        //插入数据
        list.add(1,"2002");
        System.out.println("list = " + list);
        //contains   判断是否包含
        boolean b = list.contains("202");
        //查询元素的位置
        int index = list.indexOf("2002");
        System.out.println("index = " + index);
        //集合长度
        int size = list.size();
        System.out.println("size = " + size);

    }

}

//将所有的学生保存到list中
class Student{
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}



/**
 * List: 列表   可以保存多个数据
 *    List---->Collection----->Iterable
 *   List:
 *      ArrayList : 动态数组    获取效率高     元素有序  可重复
 *      LinkedList:  链表       修改、删除   使用参考ArrayList
 *   <> 泛型   指定什么类型就是什么类型
 */

